﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace D2DEngine.Core
{
    public class D2DObjectDatabase
    {
        static D2DObjectDatabase instance = new D2DObjectDatabase();
        List<D2DObject> objects;                

        private D2DObjectDatabase()
        {
            objects = new List<D2DObject>();
        }

        /// <summary>
        /// The unique instance of the object database
        /// </summary>
        public static D2DObjectDatabase Instance
        {
            get { return instance; }
        }

        /// <summary>
        /// Inserts an object in the database
        /// </summary>
        /// <param name="obj"></param>
        public void Insert(D2DObject obj)
        {
            this.objects.Add(obj);
        }


        /// <summary>
        /// Returns a copy of the object found
        /// </summary>
        /// <typeparam name="T">The template name of the object</typeparam>
        /// <returns></returns>
        public T CloneObject<T>(string name) where T : D2DObject
        {            
            foreach (D2DObject obj in this.objects)
            {
                if (obj.Name == name)
                {
                    return (T)obj.Clone();
                }
            }

            return default(T);
        }

        /// <summary>
        /// Returns the object found.
        /// </summary>
        /// <typeparam name="T">The template name of the object</typeparam>
        /// <returns></returns>
        public T FindObject<T>(string name) where T : D2DObject
        {
            T foundObject = default(T);
            foreach (D2DObject obj in this.objects)
            {
                if (obj.Name == name)
                {
                    foundObject = obj as T;

                    if (foundObject != null)
                    {
                        return foundObject;
                    }
                }                
            }

            return default(T);
        }
    }
}
